package cn.jji8.createX.manipulate;

import cn.jji8.createX.Main;
import cn.jji8.createX.function.fill.fill;
import cn.jji8.createX.function.wand.Wand;
import cn.jji8.patronSaint.quick.boxesMenu.CanSavedMenuManger;
import cn.jji8.patronSaint.quick.boxesMenu.MenuManger;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Menu {
    //无权限提示
    public static Boolean NoPermissionPrompt = true;
    static final Map<Player, CanSavedMenuManger> menuMap = new HashMap<>();
    //需要权限才能使用
    public static boolean JudgeAuthority = true;
    /**
     * 玩家按F事件
     * */
    @EventHandler
    public static void PlayerSwapHandItemsEvent(PlayerSwapHandItemsEvent a){
        if(Material.WOODEN_AXE.equals(a.getPlayer().getInventory().getItemInMainHand().getType())){
            //判断是否拥有权限
            if(JudgeAuthority){
                if(!a.getPlayer().hasPermission("CreateX.use")){
                    if(NoPermissionPrompt){
                        a.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§d你没有权限使用创作者，需要：CreateX.use"));
                    }
                    return;
                }
            }
            CanSavedMenuManger menu = menuMap.get(a.getPlayer());
            if(menu==null){
                menu = new CanSavedMenuManger(new File(Main.getMain().getDataFolder(),"menu/main/"+a.getPlayer().getName()),1,"§g创作者,欢迎你!");
                initializationMenu(menu,a.getPlayer());
                menuMap.put(a.getPlayer(),menu);
            }
            menu.open(a.getPlayer());
            a.setCancelled(true);
        }
    }
    /**
     * 用于初始化菜单
     * */
    @SuppressWarnings("unused")
    static void initializationMenu(MenuManger menu, Player player){
        {
            //填充工具
            ItemStack itemStack = new ItemStack(Material.DIAMOND_HOE);
            ItemMeta itemMeta = itemStack.getItemMeta();
            assert itemMeta != null;
            itemMeta.setDisplayName("§d填充工具");
            ArrayList<String> loer = new ArrayList<>();
            loer.add("§g使用方块填充选中的区域.");
            itemMeta.setLore(loer);
            itemStack.setItemMeta(itemMeta);
            menu.setItemAndSingleClick(0,itemStack,new fill(player));
        }
        {
            //魔棒工具
            ItemStack itemStack = new ItemStack(Material.DIAMOND_SHOVEL);
            ItemMeta itemMeta = itemStack.getItemMeta();
            assert itemMeta != null;
            itemMeta.setDisplayName("§d魔棒工具");
            ArrayList<String> loer = new ArrayList<>();
            loer.add("§g替换你右键的方块");
            itemMeta.setLore(loer);
            itemStack.setItemMeta(itemMeta);
            menu.setItemAndSingleClick(1,itemStack,new Wand(player));
        }



    }
}
